<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class LogToMysql extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'logToMysql';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'log 同步到mysql';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
//        $redis = app('redis');
//        $keys = $redis->keys("msg:log:*");
//        if($keys){
//            $reference_at =Carbon::now()->subDays(3);
//            foreach ($keys as $logs) {
//                $log = $redis->hgetall($logs);
//                //使用carbon类
//                if($log['current_at'] > $reference_at){
//                $hh = DB::table("msg_task_log")->where("msg_task_id",$log['msg_task_id'])->where('status',$log['status'])->where('addressee',$log['addressee'])->get()->first();
//                if(!$hh){
//                    $data = [
//                        "msg_task_id" =>$log['msg_task_id'],
//                        "deviced_sn" => $log['deviced_sn'],
//                        "account_email" => $log['account_email'],
//                        "msg_device_name"=>$log['msg_device_name'],
//                        "status" => $log['status'],
//                        "log" => $log['log'],
//                        "addressee"=>$log['addressee'],
//                        "msg_device_version"=>$log['msg_device_version'],
//                        "updated_at" =>$log['current_at'],
//                    ];
//                    DB::table("msg_task_log")->insert($data);
//                }
//                    $redis->del("msg:log:".$log['msg_task_id'].":".$log['addressee'].":".$log['status']);
//                }
//
//            }
//        }
        $redis = app('redis');
        $keys = $redis->keys("msg:log:*");
        $reference_at =Carbon::now()->subday(1)->toDateTimeString();
        if($keys){

            foreach ($keys as $logs) {
                $log = $redis->hgetall($logs);
                if ($log['current_at'] > $reference_at) {
                        $redis->del($logs);
                }
            }

        }
    }
}
